
<%@ page import="org.gtesoreria.Consumo" %>
<%@ page import="java.text.SimpleDateFormat" %>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <meta name="layout" content="main" />
        <g:set var="entityName" value="${message(code: 'consumo.label', default: 'Consumo')}" />
        <title><g:message code="default.list.label" args="[entityName]" /></title>
    </head>
    <body>
        <div class="nav" style="display:none">
            <span class="menuButton"><a class="home" href="${createLink(uri: '/')}"><g:message code="default.home.label"/></a></span>
            <span class="menuButton"> <g:link class="afiliados" action="home" controller="afiliado">Afiliados</g:link></span>
             <span class="menuButton"><g:link class="afiliadoBoton" controller="afiliado" action="show" id="${afiliadoInstance?.id}">${afiliadoInstance?.apellidoNombre}</g:link></span>
        </div>
        <div class="body">
            <h1>Consumos de ${afiliadoInstance?.apellidoNombre}</h1>
            <g:if test="${flash.message}">
            <div class="message">${flash.message}</div>
            </g:if>
            <div style="margin-top:10px;margin-bottom:10px;border:1px solid #999; padding:5px">
            	<g:form action="listAfiliado">
            		<g:hiddenField name="afiliadoId" value="${afiliadoInstance?.id}"></g:hiddenField>
            		<table style="border:0px">
            		<tr>
            		<td style="padding-top:8px">Desde:</td>
            		<td><g:datePicker name="from" precision="month"></g:datePicker></td>
            		</tr>
            		<tr>
            		<td style="padding-top:8px">Hasta:</td>
            		<td><g:datePicker name="to" precision="month"></g:datePicker></td>
            		</tr>
            		<tr>
            		<td>&nbsp</td>
            		<td style="text-align:right">
            			<g:submitButton name="listAfiliado" id="buscar" value="${message(code: 'default.button.buscar.label', default: 'Buscar')}" />
            		</td>
            		</tr>
            		</table>
            	</g:form>
            </div>
            <div class="list">
                <table>
                    <thead>
                        <tr>
                        	<th>${message(code: 'consumo.consumo.label', default: 'Periodo')} </th>
                        	
                        	 <th>${message(code: 'consumo.consumo.label', default: 'Codigo')} </th>
                        	
                            <th>${message(code: 'consumo.consumo.label', default: 'Consumo')} </th>
                        
                            <th><g:message code="consumo.afiliado.label" default="Proveedor" /></th>
                        
                            <th>${message(code: 'consumo.estado.label', default: 'Estado')} </th>
                        
                            <th>${message(code: 'consumo.fecha.label', default: 'Fecha')} </th>
                        
                            <th>${message(code: 'consumo.monto.label', default: 'Monto')} </th>
                            <th>${message(code: 'consumo.monto.label', default: 'Monto a descuento')} </th>
                            <th>${message(code: 'consumo.monto.label', default: 'Cuotas')} </th>
                            <th>Mover a:</th>
                        
                        </tr>
                    </thead>
                    <tbody>
                    <g:each in="${periodos}" status="i" var="periodo">
                    <tr>
                     	<td><g:formatDate date="${showPeriods[i]}" format="MM-yyyy"/></td>
                     	
                     	<g:each in="${rubros}" status="k" var="rubro">
                     			<g:if test="${k > 0}">
						            	<tr><td>&nbsp;</td>
						        </g:if>
                     			<td>${rubro}</td>
                     			<g:if test="${!consumoInstanceList[periodo][rubro]}">
						            	<td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>
						        </g:if>
                     			<g:each in="${consumoInstanceList[periodo][rubro]}" status="l" var="consumoInstance">
                     				<g:if test="${l > 0}">
						            	</tr><tr><td>&nbsp;</td><td>&nbsp;</td>
						            </g:if>
                     				<td><g:link action="show" id="${consumoInstance.id}">${fieldValue(bean: consumoInstance, field: "id")}</g:link></td>
                        
		                            <td><g:link action="show" id="${consumoInstance?.proveedor.id}">${fieldValue(bean: consumoInstance, field: "proveedor.nombre")}</g:link></td>
		                        
		                            <td>${fieldValue(bean: consumoInstance, field: "estado")}</td>
		                        
		                            <td><g:formatDate date="${consumoInstance.fecha}" format="MM-yyyy"/></td>
		                        
		                            <td style="text-align:right"><g:formatNumber number="${consumoInstance?.monto}" type="number" minFractionDigits="2" maxFractionDigits ="2" roundingMode="HALF_UP" /></td>
		                            
		                            <td style="text-align:right"><g:formatNumber number="${consumoInstance?.montoDescuento()}" type="number" minFractionDigits="2" maxFractionDigits ="2" roundingMode="HALF_UP" /></td>
		                            
		                            <td>${fieldValue(bean: consumoInstance, field: "nroCuota")} / ${fieldValue(bean: consumoInstance, field: "cantCuotas")}</td>
		                            <td style="padding:1px">
		                            <g:if test="${consumoInstance?.montoDescuento() > 0 }">
								        <g:form method="post" action="refinanciarConsumo">
								        <g:hiddenField name="from" value="${from }"/>
								        <g:hiddenField name="to" value="${to }"/>
								        <g:hiddenField name="afiliadoId" value="${afiliadoInstance?.id }"/>
								        <input type="submit"   class="myButton" name="${ "descuento_" + consumoInstance.id}" value="${message(code: 'default.button.buscar.label', default: 'Desc.')}"/>
								        <input type="submit"   class="myButton" name="${ "prestamo_" + consumoInstance.id}" value="${message(code: 'default.button.buscar.label', default: 'Prest')}"/>
								        <input type="submit"   class="myButton" name="${ "pendiente_" + consumoInstance.id}" value="${message(code: 'default.button.buscar.label', default: 'Pend.')}"/>
								        </g:form>
								    </g:if>
							        </td>
                     			</g:each>
						        </tr>
						        <tr class="subtotal">
						        <td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>
						        <td style="font-weight:bold">Subtotal:</td>
						        <td style="text-align:right"><g:formatNumber number="${subTotales[periodo][rubro]}" type="number" minFractionDigits="2" maxFractionDigits ="2" roundingMode="HALF_UP" /></td>
						        <td style="text-align:right"><g:formatNumber number="${subTotalesDesc[periodo][rubro]}" type="number" minFractionDigits="2" maxFractionDigits ="2" roundingMode="HALF_UP" /></td>
						        <td>&nbsp;</td>
						        <td style="padding:1px">
						        <g:if test="${subTotales[periodo][rubro] > 0}">
								        <g:form method="post" action="refinanciarSubtotal">
								        <g:hiddenField name="from" value="${from }"/>
								        <g:hiddenField name="to" value="${to }"/>
								        <g:hiddenField name="afiliadoId" value="${afiliadoInstance?.id }"/>
								        <input type="submit" class="myButton" value="${message(code: 'default.button.buscar.label', default: 'Desc.')}" name="${"descuento_"+ new SimpleDateFormat('MM/yyyy').format(periodo) + "_" + rubro.toString()}"/>
								        <input type="submit" class="myButton" value="${message(code: 'default.button.buscar.label', default: 'Prest.')}" name="${"prestamo_" + new SimpleDateFormat('MM/yyyy').format(periodo) + "_" + rubro.toString()}"/>
								        <input type="submit" class="myButton" value="${message(code: 'default.button.buscar.label', default: 'Pend.')}" name="${"pendiente_"+new SimpleDateFormat('MM/yyyy').format(periodo) + "_" + rubro.toString()}"/>
								        </g:form>
								</g:if>
							    </td>
						        </tr>
                     		</g:each>
                     	
                     </tr>
                     <tr  class="subperiodo">
                     			<td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>
						        <td style="font-weight:bold">Subtotal Periodo:</td>
						        <td style="text-align:right"><g:formatNumber number="${subTotales[periodo][periodo]}" type="number" minFractionDigits="2" maxFractionDigits ="2" roundingMode="HALF_UP" /></td>
						        <td style="text-align:right"><g:formatNumber number="${subTotalesDesc[periodo][periodo]}" type="number" minFractionDigits="2" maxFractionDigits ="2" roundingMode="HALF_UP" /></td>
						        <td>&nbsp;</td>
						        <td style="padding:1px">
						        <g:if test="${subTotales[periodo][periodo] > 0}">
							        <g:form method="post" action="refinanciarPeriodo" >
							        <g:hiddenField name="from" value="${from }"/>
								    <g:hiddenField name="to" value="${to }"/>
								    <g:hiddenField name="afiliadoId" value="${afiliadoInstance?.id }"/>
							        <input type="submit" class="myButton" name="${"descuento_" + new SimpleDateFormat('MM/yyyy').format(periodo) + "_" + new SimpleDateFormat('MM/yyyy').format(periodo)}" value="${message(code: 'default.button.buscar.label', default: 'Desc.')}"/>
							        <input type="submit" class="myButton" name="${"prestamo_" + new SimpleDateFormat('MM/yyyy').format(periodo) + "_" + new SimpleDateFormat('MM/yyyy').format(periodo)}" value="${message(code: 'default.button.buscar.label', default: 'Prest.')}"/>
							        <input type="submit" class="myButton" name="${"pendiente_" + new SimpleDateFormat('MM/yyyy').format(periodo) + "_" + new SimpleDateFormat('MM/yyyy').format(periodo)}" value="${message(code: 'default.button.buscar.label', default: 'Pend.')}"/>
							        </g:form>
							    </g:if>
						        </td>
					 </tr>
                     </g:each>
                    <tr>
                     			<td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>
						        <td style="font-weight:bold">TOTAL:</td>
						       <td style="text-align:right"><g:formatNumber number="${total}" type="number" minFractionDigits="2" maxFractionDigits ="2" roundingMode="HALF_UP" /></td>
						       <td style="text-align:right"><g:formatNumber number="${totalDescuento}" type="number" minFractionDigits="2" maxFractionDigits ="2" roundingMode="HALF_UP" /></td>
						        <td>&nbsp;</td>
						        <td style="padding:1px">
						        <g:if test="${total > 0}">
						        	<g:form method="post" action="refinanciarTotal" >
							        <g:hiddenField name="from" value="${from }"/>
									<g:hiddenField name="to" value="${to }"/>
									<g:hiddenField name="afiliadoId" value="${afiliadoInstance?.id }"/>
							        <input type="submit"  class="myButton" name="descuento_refinanciarTotal" value="${message(code: 'default.button.buscar.label', default: 'Desc.')}"/>
							        <input type="submit"  class="myButton" name="prestamo_refinanciarTotal" value="${message(code: 'default.button.buscar.label', default: 'Prest.')}"/>
							        <input type="submit" class="myButton"  name="pendiente_refinanciarTotal" value="${message(code: 'default.button.buscar.label', default: 'Pend.')}"/>
							        </g:form>
						        </g:if>
						        </td>
					 </tr>
                    </tbody>
                </table>
            </div>
            <div class="paginateButtons">
                <g:paginate total="${consumoInstanceTotal}" />
            </div>
        </div>
    </body>
</html>
